package com.example.gyk.neteaseanimation.speedanimation;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.LinearInterpolator;

import androidx.annotation.Nullable;

/**
 * 项目名：JetpackPractice
 * 包名：com.example.gyk.jetpackpractice.speedanimation
 * 文件名：CustomSpeedView
 * 创建者：Gyk
 * 创建时间：2021/9/8 10:51
 * 描述：  TODO
 */

public class CustomSpeedView extends View {

    private final String TAG = "CustomSpeedView";
    private Context mContext;
    private int width;
    private int height;
    private Paint mFirstArcPaint;
    private Paint mSecondArcPaint;
    private Paint mThreeArcPaint;
    private Paint mPointerPaint;
    private Paint mDisColorPaint;//变色笔
    private ValueAnimator valueAnimator;
    private int x1;
    private int y1;
    private int maxAngle = 0;//设置中间图形直线变色的数量


    public CustomSpeedView(Context context) {
        this(context, null);
    }

    public CustomSpeedView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomSpeedView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        initView();
    }

    private void initView() {
        mFirstArcPaint = new Paint();
        mFirstArcPaint.setStyle(Paint.Style.STROKE);
        mFirstArcPaint.setStrokeWidth(20);
        mFirstArcPaint.setColor(Color.parseColor("#40f9fe"));

        mSecondArcPaint = new Paint();
        mSecondArcPaint.setStyle(Paint.Style.STROKE);
        mSecondArcPaint.setStrokeWidth(20);
        mSecondArcPaint.setColor(Color.parseColor("#899bb1"));

        mThreeArcPaint = new Paint();
        mThreeArcPaint.setStyle(Paint.Style.STROKE);
        mThreeArcPaint.setStrokeWidth(10);
        mThreeArcPaint.setColor(Color.parseColor("#36cdeb"));

        mPointerPaint = new Paint();
        mPointerPaint.setStyle(Paint.Style.FILL);
        mPointerPaint.setStrokeWidth(20);
        mPointerPaint.setColor(Color.parseColor("#ffffff"));

        mDisColorPaint = new Paint();
        mDisColorPaint.setStyle(Paint.Style.STROKE);
        mDisColorPaint.setStrokeWidth(20);
        mDisColorPaint.setColor(Color.parseColor("#40f9fe"));
    }

    public void startAnimal(int angle) {
        int h = 0;
        if (height > width) {
            h = width;
        } else {
            h = height;
        }
        int r = h / 4;
        Log.e(TAG, "r---" + r);
        valueAnimator = ValueAnimator.ofInt(-2, angle);//角度*angle数值就是指针转动的范围
        valueAnimator.setInterpolator(new LinearInterpolator());
        int finalH = h;

        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                int value = (int) valueAnimator.getAnimatedValue();
                x1 = (int) (width / 2 - (r * Math.cos(Math.toRadians(value * 15))));
                y1 = (int) (finalH / 2 - (r * Math.sin(Math.toRadians(value * 15))));
                maxAngle = value+7;
                postInvalidate();
            }
        });
        valueAnimator.setDuration(500);
        valueAnimator.start();
    }

    private void drawFirstArc(Canvas canvas) {
        int h = 0;
        if (height > width) {//当控件高度大于宽度时,设置高度等于宽度,目的是为了保持正方形,绘制圆弧时不会变形
            h = width;
        } else {
            h = height;
        }
        RectF rect = new RectF();
        rect.left = width / 10;
        rect.top = h / 10;
        rect.right = width - width / 10;
        rect.bottom = h - h / 10;
        //startAngle：30 表示顺时针30度角度开始
        //sweepAngle：-240表示逆时针扫过240度
        canvas.drawArc(rect, 30, -240, false, mFirstArcPaint);
    }

    private void drawSecondArc(Canvas canvas) {
        int h = 0;
        if (height > width) {
            h = width;
        } else {
            h = height;
        }
        for (int i = 0; i < 24; i++) {//分为24份，360/24=15，每个是15度，图形最美观
            if (maxAngle>0){//有变色直线的情况绘制
                if (i > 3 && i < maxAngle) {//绘制蓝色的直线
                    canvas.drawLine(width / 2, h - h / 6, width / 2, h - h / 5, mDisColorPaint);
                }else if (i>maxAngle-1&&i<21){//绘制灰色的直线
                    canvas.drawLine(width / 2, h - h / 6, width / 2, h - h / 5, mSecondArcPaint);
                }
            }else {//默认绘制中间图形直线(灰色直线)
                if (i > 3 && i < 21) {//中间图形的直线是从左侧60度开始到300度截至
                    canvas.drawLine(width / 2, h - h / 6, width / 2, h - h / 5, mSecondArcPaint);
                }
            }
            canvas.rotate(360 / 24, width / 2, h / 2);//关键点，画布旋转实现倾斜直线效果
        }
    }

    private void drawThreeArc(Canvas canvas) {
        int h = 0;
        if (height > width) {
            h = width;
        } else {
            h = height;
        }
        for (int i = 0; i < 19; i++) {
            canvas.drawLine(width / 4, h / 2, width / 4, h / 2 - 30, mThreeArcPaint);
            canvas.rotate(180 / 18, width / 2, h / 2);
        }
    }

    private void drawCircle(Canvas canvas) {
        int h = 0;
        if (height > width) {
            h = width;
        } else {
            h = height;
        }
        canvas.drawCircle(width / 2, h / 2, 50, mPointerPaint);
    }

    private void drawNeedle(Canvas canvas) {
        int h = 0;
        if (height > width) {
            h = width;
        } else {
            h = height;
        }
        canvas.save();
        canvas.drawLine(width / 2, h / 2, x1, y1, mPointerPaint);
        canvas.restore();
    }




    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(measureWidth(widthMeasureSpec), measureHeight(heightMeasureSpec));
        width = getMeasuredWidth();
        height = getMeasuredHeight();
        Log.e(TAG, "width==" + width);
        Log.e(TAG, "height==" + height);
        int h = 0;
        if (height > width) {
            h = width;
        } else {
            h = height;
        }
        int r = h / 4;
        x1 = (int) (width / 2 - (r * Math.cos(Math.toRadians(-30))));
        y1 = (int) (h / 2 - (r * Math.sin(Math.toRadians(-30))));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawNeedle(canvas);
        drawFirstArc(canvas);
        drawSecondArc(canvas);
        drawThreeArc(canvas);
        drawCircle(canvas);
    }

    private int measureWidth(int widthMeasureSpec) {
        int widthSpec = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int result;
        if (widthSpec == MeasureSpec.EXACTLY) {
            result = widthSize;
        } else {
            result = 400;
            if (widthSpec == MeasureSpec.AT_MOST) {
                result = Math.min(result, widthSize);
            }
        }
        return result;
    }

    private int measureHeight(int heightMeasureSpec) {
        int heightSpec = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int result;
        if (heightSpec == MeasureSpec.EXACTLY) {
            result = heightSize;
        } else {
            result = 400;
            if (heightSpec == MeasureSpec.AT_MOST) {
                result = Math.min(result, heightSize);
            }
        }
        return result;
    }
}
